package gui;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import utils.SynapseRandomizer;
import utils.math_utils.RandStream;
import utils.math_utils.RandStream.Rand;

public class SynapsePanel extends JPanel{
	
	private UDFPanel eeUDF;
	private UDFPanel eiUDF;
	private UDFPanel ieUDF;
	private UDFPanel iiUDF;
	private JTabbedPane UDFPanels;
	private JTabbedPane synapseTypes;
	private RandomizerPanel eeW;
	private RandomizerPanel eiW;
	private RandomizerPanel ieW;
	private RandomizerPanel iiW;
	
	private BoxLayout layout;
	
	private boolean recurrent;
	
	private SynapseRandomizer eeRand;
	private SynapseRandomizer eiRand;
	private SynapseRandomizer ieRand;
	private SynapseRandomizer iiRand;
	
	public SynapsePanel(SynapseRandomizer eeRand, SynapseRandomizer eiRand,
			SynapseRandomizer ieRand, SynapseRandomizer iiRand){
		
		this.eeRand = eeRand;
		this.ieRand = ieRand;
		this.eiRand = eiRand;
		this.iiRand = iiRand;
		fillFieldValuesDefault();
		layout = new BoxLayout(this, BoxLayout.X_AXIS);
		this.setLayout(layout);
		initializeLayout();
	}
	
	public void initializeLayout() {		
		
		synapseTypes = new JTabbedPane();
		synapseTypes.addTab("EE", eeW);
		synapseTypes.addTab("EI", eiW);
		synapseTypes.addTab("IE", ieW);
		synapseTypes.addTab("II", iiW);
		
		UDFPanels = new JTabbedPane();
		UDFPanels.addTab("EE", eeUDF);
		UDFPanels.addTab("EI", eiUDF);
		UDFPanels.addTab("IE", ieUDF);
		UDFPanels.addTab("II", iiUDF);
		
		Border b = BorderFactory.createLineBorder(Color.gray);
		
		TitledBorder tb = BorderFactory.createTitledBorder(b, "Synapse Parameters");
		
		synapseTypes.setBorder(tb);
		
		tb = BorderFactory.createTitledBorder(b, "UDF Parameters");
		
		UDFPanels.setBorder(tb);
		
		tb = BorderFactory.createTitledBorder(b, "Abs. Conductance (nA)");
		eeW.setBorder(tb);
		eiW.setBorder(tb);
		ieW.setBorder(tb);
		iiW.setBorder(tb);	
		
		this.add(synapseTypes);
		this.add(UDFPanels);
		
	}
	
	
	public void fillFieldValuesDefault() {
		if(recurrent) {
			eeW = new RandomizerPanel(new RandStream(Rand.GAUSS,
					30, 15, 1, 100, true));
			eiW = new RandomizerPanel(new RandStream(Rand.GAUSS,
					60, 30, 1, 200, true));
		} else {
			eeW = new RandomizerPanel(new RandStream(Rand.GAUSS,
					18, 9, 1, 54, true));
			eiW = new RandomizerPanel(new RandStream(Rand.GAUSS,
					9, 4.5, 0.5, 36, true));
		}
		ieW = new RandomizerPanel(new RandStream(Rand.GAUSS, 19, 5, 1, 38, true));
		iiW = new RandomizerPanel(new RandStream(Rand.GAUSS, 19, 5, 1, 38, true));
		
		eeUDF = new UDFPanel(eeRand, true, true);
		eiUDF = new UDFPanel(eiRand, true, false);
		ieUDF = new UDFPanel(ieRand, false, true);
		iiUDF = new UDFPanel(iiRand, false, false);
	}
	
	public static void main(String [] args) {
		
		JFrame master = new JFrame();
		master.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		master.add(new SynapsePanel(new SynapseRandomizer(),
				new SynapseRandomizer(), new SynapseRandomizer(),
				new SynapseRandomizer()), BorderLayout.CENTER);
		
		master.pack();
		master.setVisible(true);
		
	}
	
}
